import {PassThrough} from 'stream';

export class SSEStream extends PassThrough {
    constructor() {
        super({
            writableObjectMode: true,
        });
    }

    write(chunk, cb){
        super.write(`data: ${chunk}\n\n`);
    }

    // _transform(data, _encoding, done) {
    //     // data = {timestamp: data}
    //     // this.push(`data: ${JSON.stringify(data)}\n\n`);
    //     this.push(`data: ${data}\n\n`);
    //     done();
    // }
}

/**
 * Common JS 要用如下写法（注意其`导入`和`导出`的方式）：
 *
 * const {PassThrough} = require('stream');
 *
 * class SSEStream extends PassThrough {
 *     ...
 * }
 *
 * module.exports = {
 *     SSEStream: SSEStream,
 * }
 */